package com.localjoycommunity.cpdj.project.system.controller;

import com.localjoycommunity.cpdj.common.interceptor.help.NeedLogin;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;
import com.localjoycommunity.cpdj.project.system.domain.Building;
import com.localjoycommunity.cpdj.project.system.domain.TwoNewOrganization;
import com.localjoycommunity.cpdj.project.system.domain.UserCompany;
import com.localjoycommunity.cpdj.project.system.service.IBuildingService;
import com.localjoycommunity.cpdj.project.system.service.ITwoNewOrganizationService;
import com.localjoycommunity.cpdj.project.system.service.IUserCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 两新组织Controller
 *
 * @author aqie
 * @date 2020-11-04
 */
@RestController
@RequestMapping("/system/organization")
public class TwoNewOrganizationController extends BaseController {
    @Autowired
    private ITwoNewOrganizationService twoNewOrganizationService;
    @Autowired
    private IUserCompanyService userCompanyService;
    @Autowired
    private IBuildingService buildingService;

    /**
     * 查询楼宇列表
     */
    @GetMapping("/building/list")
    public AjaxResult list() {
        Building building = new Building();
        List<Building> list = buildingService.selectBuildingList(building);
        return AjaxResult.success(list);
    }

    /**
     * 用户收藏两新组织
     */
    @NeedLogin
    @PostMapping("digTwoNew")
    public AjaxResult digTwoNew(@RequestBody UserCompany userCompany, HttpServletRequest req) {
        Integer userId = getUserId(req);
        userCompany.setUserId(userId);
        int i = userCompanyService.twoNewDig(userCompany);
        return AjaxResult.success(i);
    }

    /**
     * 查询两新组织列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TwoNewOrganization twoNewOrganization) {
        startPage();
        List<TwoNewOrganization> list = twoNewOrganizationService.selectTwoNewOrganizationList(twoNewOrganization);
        return getDataTable(list);
    }

    /**
     * 导出两新组织列表
     */
    @Log(title = "两新组织", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TwoNewOrganization twoNewOrganization) {
        List<TwoNewOrganization> list = twoNewOrganizationService.selectTwoNewOrganizationList(twoNewOrganization);
        ExcelUtil<TwoNewOrganization> util = new ExcelUtil<TwoNewOrganization>(TwoNewOrganization.class);
        return util.exportExcel(list, "organization");
    }

    /**
     * 获取两新组织详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id, HttpServletRequest req) {
        TwoNewOrganization twoNewOrganization = twoNewOrganizationService.selectTwoNewOrganizationById(id);
        Integer userId = getUserId(req);
        if (userId != 0) {
            Integer dig = userCompanyService.isDig(userId, id);
            twoNewOrganization.setIsDig(dig);
            twoNewOrganization.setBrowseNum(twoNewOrganization.getBrowseNum() == null ? 1 : twoNewOrganization.getBrowseNum() + 1);
            twoNewOrganizationService.updateTwoNewOrganization(twoNewOrganization);
        }
        return AjaxResult.success(twoNewOrganization);
    }

    /**
     * 新增两新组织
     */
    @Log(title = "两新组织", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TwoNewOrganization twoNewOrganization) {
        return toAjax(twoNewOrganizationService.insertTwoNewOrganization(twoNewOrganization));
    }

    /**
     * 修改两新组织
     */
    @Log(title = "两新组织", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TwoNewOrganization twoNewOrganization) {
        return toAjax(twoNewOrganizationService.updateTwoNewOrganization(twoNewOrganization));
    }

    /**
     * 删除两新组织
     */
    @Log(title = "两新组织", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(twoNewOrganizationService.deleteTwoNewOrganizationByIds(ids));
    }
}
